using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NetMonitor.Things;
using System.Reflection;

namespace NetMonitor
{
    public partial class ThingProperties : Form
    {       
        public Thing thing;
        public ThingProperties(Thing _thing)
        {
            InitializeComponent();
            thing = _thing;
            Type type = thing.GetType();            
            FieldInfo[] fia = type.GetFields();
            foreach (FieldInfo fi in fia)
                if (fi.IsPublic && 
                    fi.Name != "x" &&
                    fi.Name != "y" &&
                    fi.Name != "w" &&
                    fi.Name != "h" &&
                    fi.Name != "id" &&
                    fi.Name != "parentThingId" &&
                    fi.Name != "col" &&
                    fi.Name != "selected" &&
                    fi.Name != "lastRunTime" &&
                    fi.Name != "queued" 
                    )
                {                    
                    Panel panel = new Panel();
                    panel.Width = 300;
                    panel.Height = 22;
                    Label label = new Label();
                    label.Text = fi.Name;
                    //label.Text = fi.FieldType.Name;
                    label.TextAlign = ContentAlignment.MiddleRight;
                    TextBox textbox = new TextBox();
                    textbox.Name = fi.Name;
                    textbox.Text = fi.GetValue(thing).ToString();
                    textbox.Location = new Point(100, 0);
                    textbox.Width = 200;
                    panel.Controls.Add(label);
                    panel.Controls.Add(textbox);
                    flowLayoutPanel1.Controls.Add(panel);
                }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            Type type = thing.GetType();
            FieldInfo[] fia = type.GetFields();
            foreach (FieldInfo fi in fia)
                if (fi.IsPublic &&
                    fi.Name != "x" &&
                    fi.Name != "y" &&
                    fi.Name != "w" &&
                    fi.Name != "h" &&
                    fi.Name != "id" &&
                    fi.Name != "parentThingId" &&
                    fi.Name != "col" &&
                    fi.Name != "selected" &&
                    fi.Name != "lastRunTime" &&
                    fi.Name != "queued"
                    )
                {
                    Control[] ca = this.Controls.Find(fi.Name, true); // true = search all children
                    if (ca.Length > 0)
                        switch (fi.FieldType.Name)
                        {
                            case "String":
                                fi.SetValue(thing, ((TextBox)ca[0]).Text);
                                break;
                            case "Int32":
                                int ret = 0;
                                int.TryParse(((TextBox)ca[0]).Text, out ret);
                                fi.SetValue(thing, ret);
                                break;
                            case "Boolean":
                                if (((TextBox)ca[0]).Text.ToLower() == "true")
                                    fi.SetValue(thing, true);
                                else
                                    fi.SetValue(thing, false);
                                break;
                        }                   
                }      
            Close();
            return;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            
            Close();
        }
    }
}